package com.cndcare.msc.gui;

import com.cndcare.msc.controller.LogUtil;
import com.cndcare.msc.handler.ClientChannel;
import com.cndcare.msc.protobuf.CmdEnum;
import com.cndcare.msc.protobuf.MessageVo;
import com.cndcare.msc.util.IPUtils;
import com.cndcare.msc.util.LocalMac;
import com.melloware.jintellitype.JIntellitype;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;

import javax.swing.*;
import java.awt.*;
import java.util.Properties;

/**
 * 利用JIntellitype实现全局热键设置
 */
@Slf4j
public class GlobleHotKeyDemo extends JFrame {

    private static final long serialVersionUID = 1L;

    //定义热键标识，用于在设置多个热键时，在事件处理中区分用户按下的热键
    public static final int FUNC_KEY_MARK = 1;
    public static final int HIDE_TIP = 2;
    public static final int SHOW_TIP = 3;
    public static boolean flag = false;

    public static GlobleHotKeyDemo getInstance() {
        return GlobleHotKeyDemo.Singleton.INSTANCE.getInstance();
    }

    private enum Singleton {
        INSTANCE;

        private GlobleHotKeyDemo singleton;

        // JVM保证这个方法绝对只调用一次
        Singleton() {
            singleton = new GlobleHotKeyDemo();
        }

        public GlobleHotKeyDemo getInstance() {
            return singleton;
        }
    }

    JButton msgBtn;
    JButton exitBtn;

    public GlobleHotKeyDemo() {
        this.setTitle("全局热键设置");
        this.setBounds(100, 100, 600, 400);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        msgBtn = new JButton("弹出框（Alt+S）");
        //设置按钮边距
        msgBtn.setMargin(new Insets(0, 0, 0, 0));
        msgBtn.setFocusable(false);
        msgBtn.setBounds(20, 20, 120, 30);
        msgBtn.addActionListener(e -> showMessage());
        this.add(msgBtn);

        exitBtn = new JButton("退出（Alt+Q）");
        exitBtn.setMargin(new Insets(0, 0, 0, 0));
        exitBtn.setFocusable(false);
        exitBtn.setBounds(160, 20, 120, 30);
        exitBtn.addActionListener(e -> System.exit(0));
        this.add(exitBtn);

        YamlPropertiesFactoryBean yamlMapFactoryBean = new YamlPropertiesFactoryBean();
        //可以加载多个yml文件
        yamlMapFactoryBean.setResources(new ClassPathResource("application.yml"));
        Properties properties = yamlMapFactoryBean.getObject();

        String dllPath = properties.getProperty("dll.path");

        JIntellitype.setLibraryLocation(dllPath);
        //第一步：注册热键，第一个参数表示该热键的标识，第二个参数表示组合键，如果没有则为0，第三个参数为定义的主要热键
        JIntellitype.getInstance().registerHotKey(FUNC_KEY_MARK, JIntellitype.MOD_ALT, (int) 'R');
        JIntellitype.getInstance().registerHotKey(HIDE_TIP, JIntellitype.MOD_ALT, (int) 'Q');
        JIntellitype.getInstance().registerHotKey(SHOW_TIP, JIntellitype.MOD_ALT, (int) 'W');

        //第二步：添加热键监听器
        JIntellitype.getInstance().addHotKeyListener(markCode -> {
            switch (markCode) {
                case FUNC_KEY_MARK:
                    log.info("发送告警数据: {}", LocalMac.getLocalMac());
                    ClientChannel.ctx.writeAndFlush(
                            MessageVo.builder()
                                    .mac(LocalMac.getLocalMac())
                                    .ip(IPUtils.getRealIp())
                                    .cmdEnum(CmdEnum.DANGER)
                                    .data("发送告警数据。。。")
                                    .build());

                    new Thread(() -> {
                        LogUtil.info(String.format("%s - %s - 告警",
                                LocalMac.getLocalMac(), IPUtils.getRealIp()));
                    }).start();

                    break;
                case HIDE_TIP:
                    RightCornerPopMessage.getInstance().setVisible(false);
                    break;
                case SHOW_TIP:
                    RightCornerPopMessage.getInstance().setVisible(true);
                    break;
            }
        });

        this.setVisible(flag);
    }

    public void showMessage() {
        JOptionPane.showMessageDialog(null, "就算把窗口最小化，按快捷键Alt+S也可以弹出提示框哦！", "弹出框标题", JOptionPane.INFORMATION_MESSAGE);
    }

    public static void main(String[] args) {
        new GlobleHotKeyDemo();
    }
}
